﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using CoDbObject;
using Colligate;
using SZJT.Utility.DataSource;

namespace Colligate.DataSource.DriverTraining.Business.Vehicle
{
    class DataNode_BusinessVehicle : BaseDataTree
    {
        private const string DATATYPE_VEHICLE_INFO = "BizVehicleInfo";
        private const string DATATYPE_VEHICLE_QUERY = "BizVehicleInfoQuery";

        public DataNode_BusinessVehicle(string taskGuid, IDataSourceLayer dataLayer, IDataService dataService)
            : base(taskGuid, dataLayer, dataService)
        {
            _dataTypeList[DataTypeCategory.SET_DATA_LIST] = new string[] { DATATYPE_VEHICLE_INFO };
            _dataTypeList[DataTypeCategory.GET_DATA_LIST] = new string[] { DATATYPE_VEHICLE_INFO };
            _dataTypeList[DataTypeCategory.TRANSFORM_LIST] = new string[] { DATATYPE_VEHICLE_QUERY };
            _dataDefine = new BusinessVehicleInfo(dataLayer);
        }

        #region Public Method
        public override int SetData(OUDataInfo UserInfo, string DataGuid, string DataType, System.Xml.XmlNode XmlData)
        {
            int nResult = 1001;
            switch (DataType)
            {
                case DATATYPE_VEHICLE_INFO:
                    nResult = SetVehicleInfo(UserInfo, DataGuid, XmlData);
                    break;
            }
            return nResult;
        }

        public override XmlNode GetData(OUDataInfo UserInfo, string DataGuid, string DataType)
        {
            XmlNode ResultInfo = null;
            switch (DataType)
            {
                case DATATYPE_VEHICLE_INFO:
                    ResultInfo = GetVehicleInfo(UserInfo, DataGuid);
                    break;
            }

            return ResultInfo;
        }


        public override int DeleteData(OUDataInfo userInfo, string dataGuid, string dataType)
        {
            int result = 1001;
            switch (dataType)
            {
                case DATATYPE_VEHICLE_INFO:
                    result = DeleteStudentInfo(userInfo, dataGuid);
                    break;
            }
            return result;
        }
      
        public override XmlNode TransformData(OUDataInfo UserInfo, string DataType, XmlNode XmlParam)
        {
            XmlNode ResultInfo = null;
            switch (DataType)
            {
                case DATATYPE_VEHICLE_QUERY:
                    ResultInfo = GetVehicleList(UserInfo, XmlParam);
                    break;
            }

            return ResultInfo;
        }

        #endregion


        #region  Private Method
        //驾校车辆基本信息
        //<!-- 应用标识：智慧驾培学事管理系统 -->
        //<!-- 模块分类：驾校运营 -->
        //<!-- 功能分类：车辆管理 -->
        //<!-- 分类序号：1001 -->
        //<!-- 数据编号：ZHJP-BUSINESS-VEHICLE-1001 -->
        //<!-- 数据类型：元数据 -->
        //<!-- 数据名称：驾校车辆基本信息 -->
        //<!-- 数据代码：BizVehicleInfo -->
        //<!-- 数据说明：驾校车辆的基本信息 -->
        //<Document TaskGuid="{智慧驾培学事管理系统任务标识}" DataGuid="{驾校车辆基本信息标识}" DataType="BizVehicleInfo">
        //    <VehicleGuid Type="GUID" Mandatory="是">{驾校车辆基本信息标识}</VehicleGuid>
        //    <SchoolGuid Type="GUID" Mandatory="是" Length="36">{单位标识}</SchoolGuid>
        //    <SchoolName Type="TEXT" Mandatory="是" Length="50">{单位名称}</SchoolName>
        //    <VehicleLicense Type="TEXT" Mandatory="是" Length="10">{车牌号}</VehicleLicense>
        //    <VehicleUnit Type="TEXT" Mandatory="否" Length="20">{车辆型号}</VehicleUnit>
        //    <VehicleStatus Type="TEXT" Mandatory="是" Length="10">{车辆状态[可用|报废|停用]}</VehicleStatus>
        //    <VehiclePhone Type="TEXT" Mandatory="否" Length="15">{车载电话号码}</VehiclePhone>
        //</Document>
        protected int SetVehicleInfo(OUDataInfo UserInfo, string VehicleGuid, XmlNode XmlData)
        {
            //if (!CheckPrivilege(UserInfo, "操作成员"))
            //    return -3;


            IDBDataObject InfoData = (_dataDefine as BusinessVehicleInfo).GetVehicleInfoData(VehicleGuid);

            int nResult = 0;
            InfoData["fVehicleGuid"] = XmlNodeManage.GetSubValue(XmlData, "VehicleGuid");
            InfoData["fSchoolGuid"] = XmlNodeManage.GetSubValue(XmlData, "SchoolGuid");
            InfoData["fSchoolName"] = XmlNodeManage.GetSubValue(XmlData, "SchoolName");
            InfoData["fVehicleLicense"] = XmlNodeManage.GetSubValue(XmlData, "VehicleLicense");
            InfoData["fVehicleUnit"] = XmlNodeManage.GetSubValue(XmlData, "VehicleUnit");
            InfoData["fVehicleStatus"] = XmlNodeManage.GetSubValue(XmlData, "VehicleStatus");
            InfoData["fVehiclePhone"] = XmlNodeManage.GetSubValue(XmlData, "VehiclePhone");

            nResult = InfoData.SaveData() ? 0 : 1;
            return nResult;
        }

        protected XmlNode GetVehicleInfo(OUDataInfo UserInfo, string VehicleGuid)
        {
            //if (!CheckPrivilege(UserInfo, "操作成员"))
            //    return null;

            IDBDataObject InfoData = (_dataDefine as BusinessVehicleInfo).GetVehicleInfoData(VehicleGuid);
            XmlNode xmlNotesInfoData = XmlNodeManage.GetDocumentNode(_taskGuid, VehicleGuid, DATATYPE_VEHICLE_INFO);

            XmlNodeManage.SetSubValue(xmlNotesInfoData, "VehicleGuid", "GUID", InfoData["fVehicleGuid"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "SchoolGuid", "GUID", InfoData["fSchoolGuid"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "SchoolName", "TEXT", InfoData["fSchoolName"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "VehicleLicense", "TEXT", InfoData["fVehicleLicense"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "VehicleUnit", "TEXT", InfoData["fVehicleUnit"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "VehicleStatus", "TEXT", InfoData["fVehicleStatus"]);
            XmlNodeManage.SetSubValue(xmlNotesInfoData, "VehiclePhone", "TEXT", InfoData["fVehiclePhone"]);

            return xmlNotesInfoData;
        }
    

        protected XmlNode GetVehicleList(OUDataInfo UserInfo, XmlNode XmlParam)
        {

            string queryLicence = XmlNodeManage.GetSubValue(XmlParam, "VehicleLicense");
            string queryUnit = XmlNodeManage.GetSubValue(XmlParam, "VehicleUnit");
            string queryStatus = XmlNodeManage.GetSubValue(XmlParam, "VehicleStatus");
            string queryPhone = XmlNodeManage.GetSubValue(XmlParam, "VehiclePhone");
            string queryPageIndex = XmlNodeManage.GetSubValue(XmlParam, "PageIndex");
            string queryPageSize = XmlNodeManage.GetSubValue(XmlParam, "PageSize");

            IDBBigObjectSet vehicleList = (_dataDefine as BusinessVehicleInfo).GetVehileList(queryLicence, queryUnit, queryStatus, queryPhone);
            Pagination dataPage = new Pagination(vehicleList);
            List<IDBDataObject> vehiclePaging = dataPage.GetPageDataSet(queryPageIndex, queryPageSize);
            queryPageIndex = dataPage.PageIndex.ToString();
            queryPageSize = dataPage.PageSize.ToString();

            XmlNode xmlQueryData = XmlNodeManage.GetDocumentNode(_taskGuid, "", DATATYPE_VEHICLE_QUERY);

            //Add the paging nodes.
            XmlNodeManage.SetSubValue(xmlQueryData, "RecordCount", "LONG", vehicleList.DataCount);
            XmlNodeManage.SetSubValue(xmlQueryData, "PageSize", "LONG", queryPageSize);
            XmlNodeManage.SetSubValue(xmlQueryData, "PageIndex", "LONG", queryPageIndex);

            XmlNode vehicleRoot = XmlNodeManage.CreateNode("VehicleList");
            XmlNode subNode;

            foreach (IDBDataObject vehicleInfo in vehiclePaging)
            {
                subNode = XmlNodeManage.CreateNode("VehicleInfo");

                XmlNodeManage.SetSubValue(subNode, "VehicleGuid", "GUID", vehicleInfo["fVehicleGuid"]);
                XmlNodeManage.SetSubValue(subNode, "VehicleLicense", "TEXT", vehicleInfo["fVehicleLicense"]);
                XmlNodeManage.SetSubValue(subNode, "VehicleUnit", "TEXT", vehicleInfo["fVehicleUnit"]);
                XmlNodeManage.SetSubValue(subNode, "VehiclePhone", "TEXT", vehicleInfo["fVehiclePhone"]);
                XmlNodeManage.SetSubValue(subNode, "VehicleStatus", "TEXT", vehicleInfo["fVehicleStatus"]);

                vehicleRoot.AppendChild(subNode);
            }
            xmlQueryData.AppendChild(vehicleRoot);
            return xmlQueryData;
        }

        protected int DeleteStudentInfo(OUDataInfo userInfo, string studentGuid)
        {
            //if (!CheckPrivilege(UserInfo, "操作成员"))
            //    return -3;

            IDBDataObject InfoData = (_dataDefine as BusinessVehicleInfo).GetVehicleInfoData(studentGuid);
            return InfoData.DeleteData() ? 0 : 1;
        }

        #endregion
    }
}
